<template>
     <div class="app-container">
  <div class="head-container">
    <crudOperation :permission="permission"/>
    <div v-if="!crud.props.searchToggle">
      </div>
      <el-dialog v-dialog-drag style="width: 80vm" title="搜索"   close-on-click-modal :visible.sync="crud.props.searchToggle"
        width="70%">
        <el-form size="small" class="formClass" label-width="85px" >
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" >
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startDate"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endDate"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="单据号">
              <el-input v-model="query.orderCode" clearable placeholder="请输入单号" />
            </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="客户名称">
                <table-select
                  :tableColumns="
                  [
                  {prop:'code',label:'客户编码',width: '100'},
                  {prop:'name',label:'客户名称',width: '150'},
                  {prop:'address',label:'地址'},
                  {prop:'sCustomerName',label:'二批'},
                  ]"
                  labelKey="name"
                  valueKey="id"
                  width="700"
                  :value.sync="query.customerIds"
                  placeholder="请选择客户"
                  url="api/dropDown/basCustomerList"
                ></table-select>
              </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="客户区域">
              <treeselect
                v-model="query.customerAreaIds"
                :options="areaList"
                placeholder="客户区域"
                :normalizer="normalizer"
                multiple
                @input="val=>getNoStr(val,'areaList','customerAreaIds')"
              />
          </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="客户分类">
                <treeselect
                v-model="query.customerTypeIds"
                :options="typeList"
                placeholder="客户分类"
                :normalizer="normalizer"
                multiple
                @input="val=>getNoStr(val,'typeList','customerTypeIds')"
              />
              </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="业务员">
                <table-select
                  :tableColumns="[{prop:'name',label:'人员名称',width: '280'}]"
                  :value.sync="query.userIds"
                  placeholder="请选择业务员"
                  url="/api/dropDown/sysUserList"
                ></table-select>
              </el-form-item>
          </el-col>

          </el-row>
          <el-row style="text-align: center;margin-top: 10px">
            <rrOperation :crud="crud"/>
          </el-row>
        </el-form>
      </el-dialog>
    <el-table ref="table" stripe border :data="crud.data" size="small" style="width: 100%;" @row-click="crud.selectRow" height="700"
    :summary-method="sumRow" show-summary>
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
        <el-table-column prop="busDate" label="日期" />
        <el-table-column prop="orderCode" label="费用支付单据号" />
        <el-table-column prop="bankAccount" label="收款账户" />
        <el-table-column prop="customerName" label="客户名称" />
        <el-table-column prop="supplierName" label="供应商名称" />
        <el-table-column prop="feeAmount" label="费用金额" />
        <el-table-column prop="ownAmount" label="本公司费用金额" />
        <el-table-column prop="supplierAmount" label="供应商费用金额" />
        <el-table-column prop="remark" label="备注" />
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import { getAreaTree } from '../../../api/erp/basCustomerArea';
import {getCustomerTypeTree} from "@/api/erp/basCustomerType";
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import crudOperation from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
const defaultCrud = CRUD({ 
    title: '客户应收报表',
    url: '/api/reportFeePay', sort: '', crudMethod: {  },
    queryOnPresenterCreated:false,
    optShow: {
    add: false,
    edit: false,
    del: false,
    // download: true,
  },
  })
const defaultForm = { }
export default {
    name: 'ReportFeePay',
    components: { pagination,crudOperation,rrOperation,Treeselect},
    mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
data(){
    return {
      permission: {
        add: ['admin', 'reportFeePay:add'],
        export: ['admin', 'reportFeePay:export']
      },
      typeList: [],
      areaList: []
    }
},
updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  created() {
    this.getTypeList()
    this.getAreaList()
  },
  mounted() {
    this.crud.props.searchToggle =false
  },
methods:{
    sumRow({ columns, data }){
      console.log(this.crud)
      const sums = []
      for (let i in  columns) {
        if (['feeAmount', 'ownAmount', 'supplierAmount'].includes(columns[i].property)) {
          let selectSum = 0
          this.$refs.table.selection.forEach(item => {
            console.log( item[columns[i].property])
            selectSum += item[columns[i].property]
          })
          sums[i] = <p>{selectSum.toFixed(2)*1}
            <hr/>
            {this.crud.summary[columns[i].property] || 0}</p>
        }
        sums[0] = <p>选中
          <hr/>
          合计</p>
      }
      return sums
    },
    getTypeList() {
      getCustomerTypeTree({page: 0, size: 999}).then(res => {
        this.typeList = res
      })
    },
    getAreaList() {
      getAreaTree().then(res => {
        this.areaList = res
      })
    },
    getNoStr(val, type, prop) {
      let str = "";
      val.forEach((item) => {
        const itemNode = this.getTreeName(this[type], item);
        str = this.getNode(itemNode, str);
      });
      this.query[prop] = str;
    },
    getTreeName(treeList, id) {
      let _this = this;
      for (let i = 0; i < treeList.length; i++) {
        let treeItem = treeList[i];
        if (treeItem.id === id) {
          return treeItem;
        } else {
          if (treeItem.children && treeItem.children.length > 0) {
            let res = _this.getTreeName(treeItem.children, id);
            if (res) {
              return res;
            }
          }
        }
      }
    },
    getNode(node, str) {
      if (node.children) {
        for (let i = 0; i < node.children.length; i++) {
          str = this.getNode(node.children[i], str);
        }
      }
      return (str += node.id + ",");
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
}
}
</script>

<style>

</style>